package com.fjwt.gz.core.jwt;

import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.core.model.security.GzUserDetails;
import lombok.Data;

import java.util.Map;

/*
* JWT payload 载体
* 格式：
    {
        "sysUserId": "10001",
        "created": "1568250147846",
        "cacheKey": "KEYKEYKEYKEY",
    }
* @author wg
* @site https://pt.jdyd.xyz/
* @date 2021/6/8 18:01
*/
@Data
public class JWTPayload {

    // 登录页面（终端）类型
    // WEB - 浏览器页面登录,  APP- APP登录,  LITE-小程序
    public interface LOGIN_PAGE_TYPE {
        String WEB = "WEB";
        String APP = "APP";
        String LITE = "LITE";
    }

    /** 认证类型: 区分哪种方式登录的 **/
    public interface CREDENTIAL_AUTH_TYPE{
        String PASSWORD = "PWD"; // 密码认证方式
        String TEL_CODE = "TELCODE"; // 手机验证码认证
    }

    private Long sysUserId;       //登录用户ID
    private Long created;         //创建时间, 格式：13位时间戳
    private String cacheKey;      //redis保存的key
    private String loginType;     // 登录页面（终端）类型

    protected JWTPayload(){}

    public JWTPayload(GzUserDetails gzUserDetails){

        this.setSysUserId(gzUserDetails.getSysUser().getSysUserId());
        this.setCreated(System.currentTimeMillis());
        this.setCacheKey(gzUserDetails.getCacheKey());
        this.setLoginType(gzUserDetails.getLoginType());

    }


    /** toMap **/
    public Map<String, Object> toMap(){

        JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(this));
        return json.toJavaObject(Map.class);
    }

    // 判断是否app登录
    public boolean isAppLogin(){
        return LOGIN_PAGE_TYPE.APP.equals(getLoginType()) || LOGIN_PAGE_TYPE.LITE.equals(getLoginType());
    }

}
